package com.offics.loan.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offics.loan.domain.entity.BankCard;
import com.offics.loan.domain.repository.BankCardRepository;
import com.offics.loan.infrastructure.persistence.mapper.BankCardMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 银行卡仓储实现类
 */
@Repository
public class BankCardRepositoryImpl extends ServiceImpl<BankCardMapper, BankCard> implements BankCardRepository {

    @Override
    public Optional<BankCard> findById(Long id) {
        return Optional.ofNullable(getById(id));
    }

    @Override
    public List<BankCard> findByUserId(Long userId) {
        LambdaQueryWrapper<BankCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankCard::getUserId, userId)
                .orderByDesc(BankCard::getIsDefault)
                .orderByDesc(BankCard::getCreateTime);
        return list(wrapper);
    }

    @Override
    public Optional<BankCard> findDefaultByUserId(Long userId) {
        LambdaQueryWrapper<BankCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankCard::getUserId, userId)
                .eq(BankCard::getIsDefault, 1);
        return Optional.ofNullable(getOne(wrapper));
    }

    @Override
    public Optional<BankCard> findByCardNo(String cardNo) {
        LambdaQueryWrapper<BankCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankCard::getCardNo, cardNo);
        return Optional.ofNullable(getOne(wrapper));
    }

    @Override
    public boolean save(BankCard bankCard) {
        return super.save(bankCard);
    }

    @Override
    public BankCard saveAndReturn(BankCard bankCard) {
        if (save(bankCard)) {
            return bankCard;
        }
        return null;
    }

    @Override
    public boolean update(BankCard bankCard) {
        return updateById(bankCard);
    }

    @Override
    public boolean deleteById(Long id) {
        return removeById(id);
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        LambdaQueryWrapper<BankCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankCard::getUserId, userId);
        return remove(wrapper);
    }

    @Override
    public boolean setDefault(Long id, Long userId) {
        // 先将所有银行卡设置为非默认
        LambdaUpdateWrapper<BankCard> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(BankCard::getUserId, userId)
                .set(BankCard::getIsDefault, 0);
        update(updateWrapper1);

        // 再将指定银行卡设置为默认
        LambdaUpdateWrapper<BankCard> updateWrapper2 = new LambdaUpdateWrapper<>();
        updateWrapper2.eq(BankCard::getId, id)
                .set(BankCard::getIsDefault, 1);
        return update(updateWrapper2);
    }

    @Override
    public boolean existsByCardNo(String cardNo) {
        LambdaQueryWrapper<BankCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankCard::getCardNo, cardNo);
        return count(wrapper) > 0;
    }
}
